package com.licode.codekit.db;

import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ResourceUtil;
import com.licode.codekit.utils.FileUtil;
import com.licode.codekit.utils.PluginUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author licode
 */
public class LiSysConfig {
    private String currentPrjDir;
    private String dbUser;
    private String dbName;
    private String dbHost;
    private String dbPort;

    private String dbPwd;
    private String dbUri;
    private String dbDriver;
    private String author;
    private int tablePrefix;
    private String dbUrlTemplate;

    private static final LiSysConfig ins = new LiSysConfig();
    private static final LiSysConfig g_ins = new LiSysConfig();

    public static LiSysConfig getInstance() {
        return ins;
    }
    public static LiSysConfig getDefault() {
        return g_ins;
    }

    public String getCurrentPrjDir() {
        return currentPrjDir;
    }

    public void setCurrentPrjDir(String prjDir) {
        this.currentPrjDir = prjDir;
    }

    public String getDbUser() {
        return dbUser;
    }

    public void setDbUser(String dbUser) {
        this.dbUser = dbUser;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getDbHost() {
        return dbHost;
    }

    public void setDbHost(String dbHost) {
        this.dbHost = dbHost;
    }

    public String getDbPort() {
        return dbPort;
    }

    public void setDbPort(String dbPort) {
        this.dbPort = dbPort;
    }

    public String getDbPwd() {
        return dbPwd;
    }

    public void setDbPwd(String dbPwd) {
        this.dbPwd = dbPwd;
    }

    public String getDbUri() {
        return dbUri;
    }

    public void setDbUri(String dbUri) {
        this.dbUri = dbUri;
    }

    public String getDbDriver() {
        return dbDriver;
    }

    public void setDbDriver(String dbDriver) {
        this.dbDriver = dbDriver;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getTablePrefix() {
        return tablePrefix;
    }

    public void setTablePrefix(int tablePrefix) {
        this.tablePrefix = tablePrefix;
    }

    public String getDbUrlTemplate() {
        return dbUrlTemplate;
    }

    public void setDbUrlTemplate(String dbUrlTemplate) {
        this.dbUrlTemplate = dbUrlTemplate;
    }

    public LiSysConfig() {
        this.init();
    }

    private void init() {
        dbName = "";
        dbHost = "localhost";
        dbPort = "3306";
        dbUser = "root";
        dbPwd = "123456";
        dbUri = "jdbc:mysql://localhost:3306/xxx?serverTimezone=Asia/Shanghai";
        // dbDriver = "c:/data/mysql-driver.jar";
        dbDriver = getDefaultDbDriver();
        tablePrefix = 0;
        author = "licode";
        dbUrlTemplate = "jdbc:mysql://{host}:{port}/{name}?serverTimezone=Asia/Shanghai";
    }

    public void resetDbDriver() {
        dbDriver = PluginUtil.getPluginDir() + "\\jdbc-driver\\mysql-driver.jar";
    }

    private String getDefaultDbDriver() {
        return PluginUtil.getPluginDir() + "\\jdbc-driver\\mysql-driver.jar";
    }

    public static String getDefaultDbConfig() {
        return PluginUtil.getPluginDir() + "\\jdbc-driver\\licode-db.xml";
    }

    private String getPrevDir(String prjDir) {
        int pos = prjDir.lastIndexOf("/");
        if (-1 == pos) {
            pos = prjDir.lastIndexOf("\\");
        }
        if (pos == -1) {
            return "";
        }

        return prjDir.substring(0, pos);
    }

    public void loadSetting() {
        boolean findDir;
        String prjDir = currentPrjDir;
        String filepath;
        do {
            filepath = prjDir + "/.idea/licode.xml";
            findDir = new File(filepath).exists();
            prjDir = getPrevDir(prjDir);

            if (prjDir.equals("")) {
                break;
            }
        } while (!findDir);

        if (!findDir) {
            filepath = currentPrjDir + "/.idea/licode.xml";
        }

        loadSetting(filepath);
    }

    public void loadDefaultSetting() {
        loadSetting(this.getDefaultDbConfig());
    }

    public void loadSetting(String filepath) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {
            DocumentBuilder builder = dbf.newDocumentBuilder();
            Document document = builder.parse(filepath);

            Node tmNode = document.getFirstChild();
            String name = tmNode.getNodeName();
            if ("setting".equals(name)) {
                tmNode = tmNode.getFirstChild();
                while (null != tmNode) {
                    name = tmNode.getNodeName();
                    if ("dbName".equals(name)) {
                        dbName = tmNode.getTextContent();
                    } else if ("dbHost".equals(name)) {
                        dbHost = tmNode.getTextContent();
                    } else if ("dbPort".equals(name)) {
                        dbPort = tmNode.getTextContent();
                    } else if ("dbUser".equals(name)) {
                        dbUser = tmNode.getTextContent();
                    } else if ("dbPwd".equals(name)) {
                        dbPwd = tmNode.getTextContent();
                    } else if ("dbUri".equals(name)) {
                        dbUri = tmNode.getTextContent();
                    } else if ("dbDriver".equals(name)) {
                        dbDriver = tmNode.getTextContent();
                    }  else if ("dbUrlTemplate".equals(name)) {
                        dbUrlTemplate = tmNode.getTextContent();
                    } else if ("tablePrefix".equals(name)) {
                        try {
                            tablePrefix = Integer.parseInt(tmNode.getTextContent());
                        } catch (Exception ignore) {
                            tablePrefix = 0;
                        }
                    }
                    tmNode = tmNode.getNextSibling();
                }
            }

            checkConfig();

        } catch (Exception ignore) {
        }
    }

    private void checkConfig() {
        if (StringUtil.isEmpty(dbUser)) {
            dbUser = "root";
        }
        if (StringUtil.isEmpty(dbPwd)) {
            dbPwd = "123456";
        }
        if (StringUtil.isEmpty(dbUri)) {
            dbUri = "jdbc:mysql://localhost:3306/xxx?serverTimezone=Asia/Shanghai";
        }
        if (StringUtil.isEmpty(dbDriver)) {
            dbDriver = getDefaultDbDriver();
        }
        if (StringUtil.isEmpty(dbUrlTemplate)) {
            dbUrlTemplate = "jdbc:mysql://{host}:{port}/{name}?serverTimezone=Asia/Shanghai";
        }
    }
}
